package com.controller;

import com.mapper.NoticeMapper;
import com.pojo.*;
import com.utils.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// 公告
@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    NoticeMapper nm;

    /**
     * 发布公告
     * @param sender 发件人 id
     * @param type 类型 0-系统公告 1-指导教师公告
     * @param content 内容
     * @param annex 附件下载地址
     */
    @RequestMapping("/release")
    public Map<String,Object> release(Integer sender,Integer type,String title,String content,String annex) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        annex = "https://mp-89d96682-3acc-41f9-8860-a1fe03e7e0cf.cdn.bspapp.com/word/"+title+".pdf";

        if (sender == null) {
            code = 500;
            msg = "发件人 id 不能为空";
        } else if (type == null) {
            code = 501;
            msg = "公告类型不能为空";
        } else if (Tool.isNull(title)) {
            code = 502;
            msg = "公告标题不能为空";
        } else if (content == null) {
            code = 503;
            msg = "公告内容不能为空";
        } else {
            nm.release(sender, type, title, content, annex);
        }

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

    /**
     * 获取公告列表
     * @param id 用户 id
     * @param role 角色 0-管理员 1-教师
     * @param type 类型 0-全部 1-我发布的
     * @param page 页码
     * @param pagesize 单页数据量
     */
    @RequestMapping("/getList")
    public Map<String,Object> getList(Integer id,Integer role,Integer type,Integer page,Integer pagesize) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (page == null) page = 0;
        if (pagesize == null) pagesize = 10;
        if (type == null) {
            code = 500;
            msg = "type 不能为空";
        } else {
            page *= pagesize;
            if (type == 0) {
                List<Notice> list = nm.getAll(page, pagesize);
                for (Notice item : list) {
                    item.setSenderName(item.getType() == 0 ? nm.getAdminById(item.getSender()).getNickname() : nm.getTeacherById(item.getSender()).getName());
                }
                map.put("data",list);
                map.put("total",nm.countAll());
            } else {
                if (id == null) {
                    code = 501;
                    msg = "id 不能为空";
                } else if (role == null) {
                    code = 502;
                    msg = "role 不能为空";
                } else {
                    List<Notice> list = nm.getList(id,role,page,pagesize);
                    for (Notice item : list) {
                        item.setSenderName(item.getType() == 0 ? nm.getAdminById(item.getSender()).getNickname() : nm.getTeacherById(item.getSender()).getName());
//                        item.setSenderName(nm.getTeacherById(item.getSender()).getName());
                    }
                    map.put("data",list);
                    map.put("total",nm.countList(id,role));
                }
            }
        }

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

    /**
     * 获取公告列表-学生
     * @param open_id 学生 id
     * @param page 页码
     * @param pagesize 单页数据量
     * @param type 公告类型 0-系统公告 1-指导教师公告
     */
    @RequestMapping("/getStudentNotice")
    public Map<String,Object> getStudentNotice(String open_id,Integer page,Integer pagesize,Integer type) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (Tool.isNull(open_id)) {
            code = 500;
            msg = "学生 id 不能为空";
        } else if (type == null) {
            code = 501;
            msg = "公告类型不能为空";
        } else {
            Student student = nm.getStudentByOpenId(open_id);
            if (student == null) {
                code = 502;
                msg = "学生 id 不存在";
            } else {
                List<Notice> list = new ArrayList<>();
                if (page == null) page = 0;
                if (pagesize == null) pagesize = 10;
                page *= pagesize;
                if (type == 0) list = nm.getAdminNotice(page, pagesize);
                else {
                    Integer teacher_id = student.getTeacher_id();
                    if (teacher_id != null) list = nm.getTeacherNotice(teacher_id,page,pagesize);
                }
                for (Notice item : list) {
                    if (item.getType() == 0) {
                        Admin admin = nm.getAdminById(item.getSender());
                        admin.setPassword(null);
                        item.setAdmin(admin);
                    } else {
                        Teacher teacher = nm.getTeacherById(item.getSender());
                        teacher.setPassword(null);
                        item.setTeacher(teacher);
                    }
                }
                map.put("data",list);
            }
        }

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

    /**
     * 删除公告
     * @param id 公告 id
     */
    @RequestMapping("/delNotice")
    public Map<String,Object> delNotice(Integer id) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (id == null) {
            code = 500;
            msg = "公告 id 不能为空";
        } else if (nm.getById(id) == null) {
            code = 501;
            msg = "公告不存在";
        } else nm.delNotice(id);

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

    /**
     * 获取公告详情
     * @param id 公告 id
     */
    @RequestMapping("/getNoticeDetails")
    public Map<String,Object> getNoticeDetails(Integer id) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (id == null) {
            code = 500;
            msg = "公告 id 不能为空";
        } else {
            Notice notice = nm.getById(id);
            if (notice == null) {
                code = 501;
                msg = "公告不存在";
            } else {
                Integer type = notice.getType();
                Integer sender = notice.getSender();
                if (type == 0) {
                    Admin admin = nm.getAdminById(sender);
                    admin.setPassword(null);
                    notice.setAdmin(admin);
                } else {
                    Teacher teacher = nm.getTeacherById(sender);
                    teacher.setPassword(null);
                    notice.setTeacher(teacher);
                }
            }
            map.put("data",notice);
        }

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

}
